<?php
//
//	file: includes/acp/acp_aucp_color.php
//	begin: 10/07/2006
//	$Author: christopher $
//	$Revision: 29 $
//	$Date: 2008-07-19 15:59:35 +0000 (Sat, 19 Jul 2008) $
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//
//	description: set color variables per group per theme

if ( !defined('IN_PHPBB') )
{
	die('Hack attempt');
}

$cp_title = 'Color_settings';
$cp_title_explain = 'Manage_colors_explain';

class acp_colors extends form
{
	var $style_id;
	var $group_id;
	var $groups;
	var $data;

	function acp_colors($requester, $parms='', &$fields)
	{
		$this->requester = $requester;
		$this->parms = empty($parms) ? array() : $parms;
		parent::form($fields);
	}

	function process()
	{
		if ($this->init())
		{
			$this->check();
			$this->validate();
		}
		if ( !_button('cancel_form') )
		{
			$this->display();
		}
	}

	function init()
	{
		global $config, $colors

		parent::init();
		$this->style_id = _read(POST_STYLES_URL, TYPE_INT, 0);
		$this->group_id = _read(POST_GROUPS_URL, TYPE_INT, 0);
		$this->groups = get_var('group_ids', array(0));
		$this->groups = empty($this->groups) ? array() : array_flip($this->groups);
		$this->data = array();

		return $this->read();
	}

	function check()
	{
		global $colors;

		// check for move
		foreach ( $this->groups as $group )
		{
			if (_button('move_up_' . $group))
			{
				$colors->set_group_weight($group, 'up');
			}
			if (_button('move_down_' . $group))
			{
				$colors->set_group_weight($group, 'down');
			}
		}

		return;
	}

	function read()
	{
		global $themes, $colors;

		$themes->read();
		if ( $this->style_id > 0 )
		{
			$this->style_name = $themes->data[$this->style_id]['style_name'];
		}
		elseif ( $this->style_id == -1 )
		{
			$this->style_name = $lang['AGCM_editing_all'];
		}
		else
		{
			return false;
		}

		$sql = 'SELECT g.group_id, g.group_name, g.group_weight, g.group_legend, g.group_color, g.group_status
					FROM ' . GROUPS_TABLE . ' g
					WHERE ( g.group_single_user <> ' . true . ' AND g.group_status < ' . GROUP_SPECIAL . ' )
						OR g.group_id IN (' . implode(', ', array_keys($colors->group_ids)) . ')
					ORDER BY g.group_weight ASC';

		if ( $this->style_id != -1 )
		{
			$sql = str_replace('SELECT ', 'SELECT c.color_code, c.bold, c.italic, c.underline, c.hover_bold, c.hover_italic, c.hover_underline, ', $sql);
			$sql = str_replace('WHERE ', 'LEFT JOIN ' . COLOR_TABLE . ' c ON c.group_id = g.group_id AND c.themes_id = ' . intval($this->style_id) . ' WHERE ', $sql);
		}

		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		while ( $row = $db->sql_fetchrow($result) )
		{
			$this->data[$row['group_id']] = $row;
			$this->total++;
		}
		$db->sql_freeresult($result);

		return true;
	}

	function validate()
	{
		global $themes, $config, $db, $colors;
		global $warning, $warning_msg;

		// nothing to do!
		if ( !_button('submit_form') )
		{
			return;
		}

		foreach ( $this->groups as $group )
		{
			$color_code = _read('color_code' . $group, TYPE_NO_HTML, '');
			$legend = empty($color_code) ? 0 : _read('legend' . $group, TYPE_INT, 0);
			$bold = empty($color_code) ? 0 : _read('bold' . $group, TYPE_INT, 0);
			$italic = empty($color_code) ? 0 : _read('italic' . $group, TYPE_INT, 0);
			$underline = empty($color_code) ? 0 : _read('underline' . $group, TYPE_INT, 0);
			$hover_bold = empty($color_code) ? 0 : _read('hover_bold' . $group, TYPE_INT, 0);
			$hover_italic = empty($color_code) ? 0 : _read('hover_italic' . $group, TYPE_INT, 0);
			$hover_underline = empty($color_code) ? 0 : _read('hover_underline' . $group, TYPE_INT, 0);
			$group_color = empty($color_code) ? 0 : 1;

			$sql = 'UPDATE ' . COLOR_TABLE . '
					SET color_code = \'' . $color_code . '\', bold = ' . $bold . ', italic = ' . $italic . ', underline = ' . $underline . ', hover_bold = ' . $hover_bold . ', hover_italic = ' . $hover_italic . ', hover_underline = ' . $hover_underline . '
					WHERE group_id = ' . $group;

			if ( $this->style_id != -1 )
			{
				$sql = str_replace('WHERE', 'WHERE themes_id = ' . $this->style_id . ' AND', $sql);
			}

			$db->sql_query($sql, false, __LINE__, __FILE__);


			$sql = 'UPDATE ' . GROUPS_TABLE . '
					SET group_legend = ' . intval($legend) . ', group_color = ' . intval($group_color) . '
					WHERE group_id = ' . $group;
			$db->sql_query($sql, false, __LINE__, __FILE__);
		}

		$agcm_check = _read('aucp_session_check', TYPE_INT, 0);

		$sql = 'UPDATE ' . CONFIG_TABLE . '
				SET config_value = ' . $agcm_check . '
				WHERE config_name = \'aucp_session_check\'';
		$db->sql_query($sql, false, __LINE__, __FILE__);

		$agcm_time = _read('aucp_session_time', TYPE_INT, 0);
		$agcm_value = _read('aucp_time_value', TYPE_INT, 1);
		$agcm_time = $agcm_time * $agcm_value;

		$sql = 'UPDATE ' . CONFIG_TABLE . '
				SET config_value = ' . $agcm_time . '
				WHERE config_name = \'aucp_session_time\'';
		$db->sql_query($sql, false, __LINE__, __FILE__);

		$agcm_value = isset($HTTP_POST_VARS['agcm_value']) ? intval($HTTP_POST_VARS['agcm_value']) : 0;

		$config->read(true);

		if ( $this->style_id != -1 )
		{
			$colors->read_theme($this->style_id, true);
		}
		else
		{
			foreach ($themes as $theme_id => $dummy)
			{
				$colors->read_theme($theme_id, true);
			}
		}

		$colors->read(true);

		$warning_msg = $lang['AGCM_update_successful'];
		$warning = true;

		return;
	}

	function display()
	{
		global $template, $user;
		global $warning, $warning_msg;

		if ( $warning )
		{
			$template->assign_block_vars('warning', array(
				'WARNING_TITLE' => $user->lang('Check_results'),
				'WARNING_MSG' => $warning_msg,
			));
		}

		if ( $this->style_id == 0 )
		{
			$this->display_select();
		}
		else
		{
			$this->display_edit();
		}

		return;
	}

	function display_select()
	{
		global $db, $template, $user, $config;

		$select_list = '<select name="' . POST_STYLES_URL . '">';
		$select_list .= '<option value="-1">' . $user->lang('AGCM_edit_all') . '</option>';

		foreach ($themes->data as $theme_id => $row)
		{
			$select_list .= '<option value="' . $theme_id . '">' . $row['style_name'] . '</option>';
		}

		$select_list .= '</select>';

		$template->assign_vars(array(
			'L_TITLE' => $lang['AGCM_color_admin'],
			'L_EXPLAIN' => $lang['AGCM_color_admin_explain'],
			'L_SELECT' => $lang['AGCM_select_style'],
			'L_LOOK_UP' => $lang['AGCM_look_up_group_color'],

			'S_ACTION' => append_sid("admin_color.$phpEx"),
			'S_SELECT' => $select_list,
		));

		$template->assign_block_vars('cp_content', array('BOX' => $template->include_file('acp/acp_aucp_color_select_box.tpl')));
	}

	function display_edit()
	{
		global $db, $template, $user, $config;

		$s_hidden_fields = '<input type="hidden" name="' . POST_STYLES_URL . '" value="' . $style_id . '" />';

		$template->assign_vars(array(
			'TIME' => $board_config['agcm_time'],

			'L_TITLE' => $lang['AGCM_color_admin'],
			'L_EDIT' => $style_name,
			'L_NO' => $lang['No'],
			'L_YES' => $lang['Yes'],
			'L_SUBMIT' => $lang['Submit'],
			'L_RESET' => $lang['Reset'],
			'L_COLOR' => $lang['AGCM_color'],
			'L_COLOR_EXPLAIN' => $lang['AGCM_color_explain'],
			'L_FIND_COLOR' => $lang['AGCM_find_color'],
			'L_MOVE_UP' => $user->lang('Move_up'),
			'I_MOVE_UP' => $user->img('cmd_up_arrow'),
			'L_MOVE_DOWN' => $user->lang('Move_down'),
			'I_MOVE_DOWN' => $user->img('cmd_down_arrow'),
			'L_LEGEND' => $lang['AGCM_display_legend'],
			'L_TIME' => $lang['AGCM_time'],
			'L_TIME_EXPLAIN' => $lang['AGCM_time_explain'],
			'L_CHECK' => $lang['AGCM_check'],
			'L_CHECK_EXPLAIN' => $lang['AGCM_check_explain'],
			'L_BOLD' => $lang['AGCM_bold'],
			'L_ITALIC' => $lang['AGCM_italic'],
			'L_UNDERLINE' => $lang['AGCM_underline'],
			'L_STYLE_EXPLAIN' => $lang['AGCM_style_explain'],
			'L_HOVER_STYLE_EXPLAIN' => $lang['AGCM_hover_style_explain'],

			'U_SEARCH_COLOR' => append_sid($phpbb_root_path . "search." . $phpEx . "?mode=searchcolor"),

			'S_VALUE' => $colors->inactive_select(),
			'S_CHECK_YES' => ($board_config['agcm_check']) ? ' checked="checked"' : '',
			'S_CHECK_NO' => (!$board_config['agcm_check']) ? ' checked="checked"' : '',
			'S_HIDDEN_FIELDS' => $s_hidden_fields,
			'S_ACTION' => append_sid("admin_color.$phpEx"),
		));

		if ( !empty($groups) )
		{
			for ($j = 0; $j < $i; $j++)
			{
				$template->assign_block_vars('group_color_edit', array(
					'ID' => $groups[$j]['group_id'],
					'GROUP_NAME' => $user->lang($groups[$j]['group_name']),
					'GROUP_COLOR' => $style_id == -1 ? '' : $groups[$j]['color_code'],
					'BOLD' => $style_id == -1 ? '' : ( intval($groups[$j]['bold']) ? ' checked="checked"' : '' ),
					'ITALIC' => $style_id == -1 ? '' : ( intval($groups[$j]['italic']) ? ' checked="checked"' : '' ),
					'UNDERLINE' => $style_id == -1 ? '' : ( intval($groups[$j]['underline']) ? ' checked="checked"' : '' ),
					'HOVER_BOLD' => $style_id == -1 ? '' : ( intval($groups[$j]['hover_bold']) ? ' checked="checked"' : '' ),
					'HOVER_ITALIC' => $style_id == -1 ? '' : ( intval($groups[$j]['hover_italic']) ? ' checked="checked"' : '' ),
					'HOVER_UNDERLINE' => $style_id == -1 ? '' : ( intval($groups[$j]['hover_underline']) ? ' checked="checked"' : '' ),

					'U_WEIGHT_UP' => append_sid("admin_color." . $phpEx . "?" . POST_GROUPS_URL . "=" . $groups[$j]['group_id'] . "&amp;edit=" . ($groups[$j]['group_weight'] - 15) . "&amp;" . POST_STYLES_URL . "=" . $style_id),
					'U_WEIGHT_DOWN' => append_sid("admin_color." . $phpEx . "?" . POST_GROUPS_URL . "=" . $groups[$j]['group_id'] . "&amp;edit=" . ($groups[$j]['group_weight'] + 15) . "&amp;" . POST_STYLES_URL . "=" . $style_id),

					'S_LEGEND_YES' => $groups[$j]['group_legend'] ? ' checked="checked"' : '',
					'S_LEGEND_NO' => !$groups[$j]['group_legend'] ? ' checked="checked"' : '',
				));

				if ( $j != 0 )
				{
					$template->assign_block_vars('group_color_edit.up', array());
				}

				if ( $j < ($i - 1) )
				{
					$template->assign_block_vars('group_color_edit.down', array());
				}
			}
		}

		// get total rows
		$sql = 'SELECT DISTINCT a.user_id
					FROM ' . ATT_LINKS_TABLE . ' a' . (empty($author) ? '' : ', ' . USERS_TABLE . ' u
					WHERE u.user_id = a.user_id
						AND u.username LIKE \'' . $db->sql_escape_string($author) . '\'');
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		$total_users = $db->sql_numrows($result);
		$db->sql_freeresult($result);
		if ( $total_users )
		{
			// get users rows
			$sql = '';
			unset($privmsgs_ids);
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);
			unset($sql);
			$i = $this->start;
			$color = false;
			while ( $row = $db->sql_fetchrow($result) )
			{
				$i++;
				$file_size = format_size(intval($row['sum_filesize']));
				$template->assign_block_vars('row', array(
					'ROW_NUMBER' => $i,
					'USERNAME' => !$row['user_id'] || ($row['user_id'] == ANONYMOUS) ? $user->lang('Guest') : $row['username'],
					'U_USERNAME' => $config->url($this->requester, array_merge($this->parms, array('ctx' => 'search', POST_USERS_URL => !$row['user_id'] ? ANONYMOUS : intval($row['user_id']))), true),
					'COUNT_FILES' => intval($row['count_attach_id']),
					'SIZE_FILES' => $file_size['value'],
					'UNIT' => $user->lang($file_size['unit']),
				));
				$color = !$color;
				$template->set_switch('row.light', $color);
			}
			$db->sql_freeresult($result);
		}

		// send all to template
		$template->assign_block_vars('cp_content', array('BOX' => $template->include_file('acp/acp_aucp_color_edit_box.tpl')));


	}
}

// instantiate the display
$parms = array(
	'mode' => $menu_id,
	'sub' => $subm_id == $menu_id ? '' : $subm_id,
	'ctx' => $ctx_id == $subm_id ? '' : $ctx_id,
);
$form = new acp_colors($cp_requester, $cp_parms + $parms, $fields);
$form->process();
$template->set_filenames(array('body' => 'cp_generic.tpl'));

?>